<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="gv-forms gv-forms-fluid" layout="column">
  <div class="gv-forms-header">
    <h1>Documentation</h1>
    <a permission permission-only="['environment-documentation-c', 'api-documentation-c']" ng-click="$ctrl.importPages()">
      Import multiple files
    </a>
    <md-button
      class="md-raised md-primary"
      type="button"
      ng-if="$ctrl.hasExternalDoc()"
      ng-click="$ctrl.fetch()"
      ng-disabled="$ctrl.fetchAllInProgress"
      permission
      permission-only="['environment-documentation-u', 'api-documentation-u']"
    >
      Fetch all
    </md-button>
  </div>
  <div class="gv-form">
    <div layout="row" layout-align="space-between center" ng-if="!renameFolder">
      <h2>
        <span ng-repeat="folder in $ctrl.breadcrumb">
          <a ng-if="$ctrl.apiId && !$last" ui-sref="management.apis.detail.portal.documentation({apiId: $ctrl.apiId, parent: folder.id})"
            >{{folder.name}}</a
          >
          <a ng-if="!$ctrl.apiId && !$last" ui-sref="management.settings.documentation({parent: folder.id})">{{folder.name}}</a>
          <span ng-if="$ctrl.apiId && $last">{{folder.name}}</span>
          <span ng-if="!$ctrl.apiId && $last">{{folder.name}}</span>
          <span ng-if="$first || !$last ">&nbsp;/&nbsp;</span>
        </span>
      </h2>
      <div ng-if="$ctrl.breadcrumb.length > 1 && $ctrl.currentFolder.type != 'SYSTEM_FOLDER'">
        <md-button
          type="button"
          class="md-raised md-primary"
          ng-click="$ctrl.changeFolderVisibility()"
          aria-label="visibility"
          permission
          permission-only="['environment-documentation-u', 'api-documentation-u']"
        >
          {{ $ctrl.currentFolder.visibility === 'PRIVATE' ? 'Make PUBLIC' : 'Make PRIVATE'}}</md-button
        >
        <md-button
          type="button"
          class="md-raised md-primary"
          ng-click="$ctrl.toggleRenameFolder()"
          aria-label="Edit"
          permission
          permission-only="['environment-documentation-u', 'api-documentation-u']"
          ng-if="!translateFolder"
        >
          Rename Folder</md-button
        >
        <md-button
          type="button"
          class="md-raised md-primary"
          ng-click="$ctrl.toggleTranslateFolder()"
          aria-label="Translations"
          permission
          permission-only="['environment-documentation-u', 'api-documentation-u']"
        >
          {{ translateFolder? 'Browse Folder' : 'Translate Folder' }}</md-button
        >
      </div>
    </div>
    <div layout="row" layout-align="space-between center" ng-if="renameFolder">
      <h2>
        <span ng-repeat="folder in $ctrl.breadcrumb">
          <span ng-if="!$last">{{folder.name}}</span>
          <input ng-if="$last" type="text" ng-model="$ctrl.newFolderName" />
          <span ng-if="$first || !$last ">&nbsp;/&nbsp;</span>
        </span>
      </h2>
      <div ng-if="$ctrl.breadcrumb.length > 1">
        <md-button type="button" class="md-raised md-primary" ng-click="$ctrl.renameFolder()" aria-label="Edit"> Save</md-button>
        <md-button class="md-raised" type="button" ng-click="$ctrl.toggleRenameFolder()"> Cancel </md-button>
      </div>
    </div>

    <!-- translation -->
    <div ng-if="translateFolder">
      <md-table-container>
        <table md-table class="gv-table-dense">
          <thead md-head>
            <tr md-row>
              <th md-column width="48px">Language</th>
              <th md-column>Name</th>
              <th md-column width="1%" nowrap>Last Update</th>
              <th md-column width="48px"></th>
              <th md-column width="48px"></th>
            </tr>
          </thead>
          <tbody md-body>
            <tr ng-if="!$ctrl.currentFolder.translations.length || $ctrl.currentFolder.translations.length <= 0">
              <td md-cell colspan="5"><span>No translation available</span></td>
            </tr>
            <tr md-row ng-repeat="translation in $ctrl.currentFolder.translations">
              <td md-cell width="48px">
                <span>
                  <a ng-click="$ctrl.selectTranslation(translation)">{{translation.configuration.lang}}</a>
                </span>
              </td>
              <td md-cell>{{translation.name}}</td>
              <td md-cell width="1%" nowrap>{{translation.lastModificationDate | date:'medium'}}</td>
              <td md-cell width="48px">
                <ng-md-icon
                  icon="delete"
                  ng-click="$ctrl.remove(translation)"
                  style="font-size: 24px; height: 24px"
                  permission
                  permission-only="['environment-documentation-u', 'api-documentation-u']"
                >
                  <md-tooltip md-direction="top" md-visible="false">Remove</md-tooltip>
                </ng-md-icon>
              </td>
            </tr>
          </tbody>
        </table>
      </md-table-container>

      <div style="text-align: right">
        <md-button
          permission
          permission-only="['environment-documentation-u', 'api-documentation-u']"
          type="button"
          aria-label="Add translation"
          class="md-raised md-primary"
          ng-click="$ctrl.addTranslation()"
        >
          Add a translation
        </md-button>
      </div>

      <div class="gv-form-content" ng-if="$ctrl.currentTranslation">
        <md-input-container class="md-block">
          <label>Language (max. 2 characters)</label>
          <input ng-model="$ctrl.currentTranslation.configuration.lang" maxlength="2" required />
        </md-input-container>
        <md-input-container class="md-block">
          <label>Translated title</label>
          <input type="text" ng-model="$ctrl.currentTranslation.name" required />
        </md-input-container>
      </div>

      <div class="md-actions gravitee-api-save-button" layout="row" style="padding-top: 32px" ng-if="$ctrl.currentTranslation">
        <md-button
          permission
          permission-only="['environment-documentation-u', 'api-documentation-u']"
          type="button"
          aria-label="Save translation"
          class="md-raised md-primary"
          ng-click="$ctrl.saveFolderTranslation()"
          ng-disabled="!$ctrl.currentTranslation.configuration.lang || !$ctrl.currentTranslation.name"
        >
          Save translation
        </md-button>
        <md-button class="md-raised" type="button" ng-click="$ctrl.refreshCurrentFolder()"> Reset </md-button>
      </div>
    </div>

    <!-- browse folder-->
    <div class="gv-form-content" layout="column" ng-if="!translateFolder">
      <md-table-container ng-if="$ctrl.pages.length > 0 || ($ctrl.systemFolders.length > 0 && $ctrl.rootDir=='') ">
        <table md-table class="gv-table-dense">
          <thead md-head>
            <tr md-row>
              <th md-column class="column-with-logo">Name</th>
              <th md-column width="48px"></th>
              <th md-column width="48px"></th>
              <th md-column width="48px"></th>
              <th md-column width="76px"></th>
              <th md-column width="1%" nowrap>Last Update</th>
              <th md-column width="48px"></th>
              <th md-column width="24px"></th>
            </tr>
          </thead>
          <tbody md-body>
            <tr md-row ng-if="!$ctrl.rootDir || $ctrl.rootDir == ''" ng-repeat="page in $ctrl.systemFolders">
              <td md-cell>
                <md-icon md-svg-src="assets/logo_{{page.type | lowercase}}.svg" style="margin-right: 12px"></md-icon>
                <span>
                  <a ng-click="$ctrl.openUrl(page)">{{page.name}}</a>
                </span>
              </td>
              <td md-cell width="48px"></td>
              <td md-cell width="48px"></td>
              <td md-cell width="48px"></td>
              <td md-cell width="76px"></td>
              <td md-cell width="1%"></td>
              <td md-cell width="48px"></td>
              <td md-cell width="24px"></td>
            </tr>
            <tr md-row ng-repeat="page in $ctrl.pages">
              <td md-cell>
                <md-icon md-svg-src="assets/logo_{{page.type | lowercase}}.svg"></md-icon>
                <sub ng-if="'LINK' === page.type" class="icon-button-homepage">
                  <ng-md-icon
                    ng-if="'external' === page.configuration.resourceType"
                    icon="open_in_new"
                    size="12"
                    style="fill: black"
                  ></ng-md-icon>
                  <ng-md-icon
                    ng-if="'page' === page.configuration.resourceType"
                    icon="insert_drive_file"
                    size="12"
                    style="fill: black"
                  ></ng-md-icon>
                  <ng-md-icon
                    ng-if="'category' === page.configuration.resourceType"
                    icon="view_comfy"
                    size="12"
                    style="fill: black"
                  ></ng-md-icon>
                  <md-tooltip>{{ page.configuration.resourceType }}</md-tooltip>
                </sub>

                <span style="margin-left: 12px">
                  <a ng-style="page.published?'':{'font-style': 'italic', 'color': 'grey'}" ng-click="$ctrl.openUrl(page)">
                    {{page.name}}
                  </a>
                  <sup ng-if="page.homepage" class="icon-button-homepage">
                    <ng-md-icon icon="home" size="12" style="fill: rgb(68, 138, 255)"></ng-md-icon>
                  </sup>
                  <sup
                    ng-if="page.translations && (page.type !== 'LINK' || (page.type === 'LINK' && page.configuration && page.configuration.inherit && page.configuration.inherit === 'false'))"
                    class="icon-button-homepage"
                  >
                    <ng-md-icon icon="language" size="12" style="fill: rgb(68, 138, 255)">
                      <md-tooltip>Translated</md-tooltip>
                    </ng-md-icon>
                  </sup>
                </span>
              </td>
              <td md-cell width="48px">
                <ng-md-icon
                  icon="link"
                  ng-click="$ctrl.createShortCut(page)"
                  style="font-size: 24px; height: 24px"
                  ng-if="$ctrl.canCreateShortCut(page.id, page.type)"
                  permission
                  permission-only="['environment-documentation-u', 'api-documentation-u']"
                >
                  <md-tooltip md-direction="top" md-visible="false">Create a Link in ...</md-tooltip>
                </ng-md-icon>
              </td>
              <td md-cell width="48px">
                <ng-md-icon
                  icon="archive"
                  ng-click="$ctrl.moveToFolder(page)"
                  style="font-size: 24px; height: 24px"
                  ng-if="page.type !== 'folder'"
                  permission
                  permission-only="['environment-documentation-u', 'api-documentation-u']"
                >
                  <md-tooltip md-direction="top" md-visible="false">Move to ...</md-tooltip>
                </ng-md-icon>
              </td>
              <td md-cell width="48px">
                <ng-md-icon
                  ng-if="!page.published && !$ctrl.isLink(page.type)"
                  icon="{{$ctrl.isMarkdownTemplate(page.type) ? 'check_box_outline_blank' : 'cloud_queue'}}"
                  ng-click="$ctrl.togglePublish(page)"
                  style="font-size: 24px; height: 24px"
                  permission
                  permission-only="['environment-documentation-u', 'api-documentation-u']"
                >
                  <md-tooltip md-direction="top" md-visible="false">
                    {{$ctrl.isMarkdownTemplate(page.type) ? 'Make template available' : 'Publish this page'}}
                  </md-tooltip>
                </ng-md-icon>
                <ng-md-icon
                  ng-if="page.published && !$ctrl.isLink(page.type)"
                  icon="{{$ctrl.isMarkdownTemplate(page.type) ? 'check_box' : 'cloud_done'}}"
                  ng-click="$ctrl.togglePublish(page)"
                  style="font-size: 24px; height: 24px"
                  permission
                  permission-only="['environment-documentation-u', 'api-documentation-u']"
                >
                  <md-tooltip md-direction="top" md-visible="false">
                    {{$ctrl.isMarkdownTemplate(page.type) ? 'Make template unavailable' : 'Unpublish this page'}}
                  </md-tooltip>
                </ng-md-icon>
              </td>
              <td md-cell width="76px" nowrap>
                <ng-md-icon
                  ng-if="!$first"
                  icon="arrow_upwards"
                  ng-click="$ctrl.upward(page)"
                  permission
                  permission-only="['environment-documentation-u', 'api-documentation-u']"
                >
                  <md-tooltip md-direction="top" md-visible="false">Move upward</md-tooltip>
                </ng-md-icon>
                <span ng-if="$first" style="padding-right: 28px"></span>
                <ng-md-icon
                  ng-if="!$last"
                  icon="arrow_downward"
                  ng-click="$ctrl.downward(page)"
                  permission
                  permission-only="['environment-documentation-u', 'api-documentation-u']"
                >
                  <md-tooltip md-direction="top" md-visible="false">Move downward</md-tooltip>
                </ng-md-icon>
              </td>
              <td md-cell width="1%" nowrap>{{page.lastModificationDate | date:'medium'}}</td>
              <td md-cell width="48px">
                <ng-md-icon
                  icon="delete"
                  ng-click="$ctrl.remove(page)"
                  style="font-size: 24px; height: 24px"
                  permission
                  permission-only="['environment-documentation-u', 'api-documentation-u']"
                >
                  <md-tooltip md-direction="top" md-visible="false">Remove</md-tooltip>
                </ng-md-icon>
              </td>
              <td md-cell width="24px">
                <ng-md-icon
                  ng-if="$ctrl.isPage(page.type) || $ctrl.isFolder(page.type)"
                  icon="{{ page.visibility === 'PRIVATE' ? 'lock' : 'public' }}"
                  size="20"
                  style="fill: #cdcdcd"
                >
                  <md-tooltip>{{ page.visibility | uppercase }}</md-tooltip>
                </ng-md-icon>
              </td>
            </tr>
          </tbody>
        </table>
      </md-table-container>

      <gravitee-empty-state
        ng-if="$ctrl.rootDir != '' && $ctrl.pages.length == 0"
        icon="insert_drive_file"
        model="Page"
        message="Documentation will appear here"
        sub-message="Start creating a page"
        create-mode="true"
      ></gravitee-empty-state>

      <md-fab-speed-dial
        md-direction="{{$ctrl.pages.length > 0 ? 'left' : 'down'}}"
        md-open="false"
        ng-class="{'md-fab-bottom-right md-fab-scrollable' : $ctrl.pages.length > 0, 'gv-help-displayed': $ctrl.$rootScope.helpDisplayed}"
        class="md-scale"
      >
        <md-fab-trigger>
          <md-button
            aria-label="menu"
            class="md-fab md-success"
            permission
            permission-only="['environment-documentation-u', 'api-documentation-u']"
          >
            <md-tooltip ng-if="$ctrl.pages.length > 0" md-direction="top" md-visible="false">New page</md-tooltip>
            <ng-md-icon icon="add"></ng-md-icon>
          </md-button>
        </md-fab-trigger>

        <md-fab-actions>
          <md-button ng-repeat="typeInfo in $ctrl.supportedTypes" class="md-fab md-raised md-mini" ng-click="$ctrl.newPage(typeInfo.type)">
            <md-tooltip md-direction="{{$ctrl.pages.length > 0 ? 'top' : 'right'}}">{{typeInfo.tooltip}}</md-tooltip>
            <md-icon md-svg-src="assets/logo_{{typeInfo.type | lowercase}}.svg"></md-icon>
          </md-button>
        </md-fab-actions>
      </md-fab-speed-dial>
    </div>
  </div>
</div>
